package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.EduActivitiesFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduPsychologicalCounsellingConvert;
import com.engine4cloud.tdf.education.dto.create.EduPsychologicalCounsellingCreateDTO;
import com.engine4cloud.tdf.education.dto.query.EduActivitiesFileQueryDTO;
import com.engine4cloud.tdf.education.dto.query.EduPsychologicalCounsellingQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduPsychologicalCounsellingResultDTO;
import com.engine4cloud.tdf.education.dto.update.EduPsychologicalCounsellingUpdateDTO;
import com.engine4cloud.tdf.education.entity.EduPsychologicalCounsellingEntity;
import com.engine4cloud.tdf.education.mapper.EduPsychologicalCounsellingMapper;
import com.engine4cloud.tdf.education.service.EduActivityFileService;
import com.engine4cloud.tdf.education.service.EduPsychologicalCounsellingService;
import com.engine4cloud.tdf.education.service.PsychologicalCounsellingAttendService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 心理辅导
 *
 * @author wenjie
 * @date 2024-08-13 16:56:33
 */
@Service
public class EduPsychologicalCounsellingServiceImpl extends ServiceImpl<EduPsychologicalCounsellingMapper, EduPsychologicalCounsellingEntity> implements EduPsychologicalCounsellingService {
    @Resource
    private EduActivityFileService activityFileService;

    @Resource
    private PsychologicalCounsellingAttendService counsellingAttendService;

    @Override
    public PageInfo<EduPsychologicalCounsellingResultDTO> pageList(Params pageable, EduPsychologicalCounsellingQueryDTO dto) {
        IPage<EduPsychologicalCounsellingEntity> page = super.page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, EduPsychologicalCounsellingConvert.INSTANCE::convert);
    }

    @Override
    public EduPsychologicalCounsellingResultDTO getById(Long id) {
        EduPsychologicalCounsellingResultDTO result = Optional.ofNullable(EduPsychologicalCounsellingConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        //设置文件
        result.setFileList(activityFileService.findList(new EduActivitiesFileQueryDTO(result.getFdbh(), EduActivitiesFileTypeEnum.PSYCHOLOGICAL_COUNSELLING)));
        return result;
    }

    @Override
    public EduPsychologicalCounsellingResultDTO findByCode(String code) {
        return EduPsychologicalCounsellingConvert.INSTANCE.convert(super.getOne(wrapper(new EduPsychologicalCounsellingQueryDTO(code))));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(EduPsychologicalCounsellingCreateDTO dto) {
        TdfUser user = SecurityUtils.getUser();
        EduPsychologicalCounsellingEntity entity = EduPsychologicalCounsellingConvert.INSTANCE.create(dto, user.getTenantId());
        super.save(entity);
        //设置文件
        Actions.apply(CollectionUtils.isNotEmpty(dto.getFileList()), () ->
                activityFileService.batchSave(new EduActivitiesFileQueryDTO(entity.getFdbh(), EduActivitiesFileTypeEnum.PSYCHOLOGICAL_COUNSELLING), dto.getFileList(), user));
        //设置参加人列表
        Actions.apply(CollectionUtils.isNotEmpty(dto.getAttendList()), () -> counsellingAttendService.batchCreate(entity.getFdbh(), dto.getAttendList()));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(EduPsychologicalCounsellingUpdateDTO dto) {
        EduPsychologicalCounsellingEntity old = Optional.ofNullable(this.baseMapper.selectById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        EduPsychologicalCounsellingEntity entity = EduPsychologicalCounsellingConvert.INSTANCE.update(dto);
        super.updateById(entity);
        //设置文件
        Actions.apply(CollectionUtils.isNotEmpty(dto.getFileList()), () -> activityFileService.batchUpdate(new EduActivitiesFileQueryDTO(old.getFdbh(), EduActivitiesFileTypeEnum.PSYCHOLOGICAL_COUNSELLING), dto.getFileList(), user));
        //设置参加人列表
        Actions.apply(CollectionUtils.isNotEmpty(dto.getAttendList()), () -> counsellingAttendService.batchUpdate(old.getFdbh(), dto.getAttendList()));
        return true;
    }

    private LambdaQueryWrapper<EduPsychologicalCounsellingEntity> wrapper(EduPsychologicalCounsellingQueryDTO dto) {
        LambdaQueryWrapper<EduPsychologicalCounsellingEntity> wrapper = Queries.lambda();
        Queries.accept(dto.getId(), v -> wrapper.eq(EduPsychologicalCounsellingEntity::getId, v));
        Queries.accept(dto.getFdbh(), v -> wrapper.eq(EduPsychologicalCounsellingEntity::getFdbh, v));
        Queries.accept(dto.getFdnr(), v -> wrapper.like(EduPsychologicalCounsellingEntity::getFdnr, v));
        wrapper.orderByDesc(EduPsychologicalCounsellingEntity::getCreateTime);
        return wrapper;
    }
}